package com.admin.education.controllers.api.v1;

import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.jwt.JwtUtils;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.integrals.IntegralsTransfer;
import com.admin.education.dataTransferObject.userInformation.UserInformationTransfer;
import com.admin.education.services.Interfaces.IIntegralsService;
import com.admin.education.services.Interfaces.IUserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.concurrent.Callable;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/v1/currentUser")
@JwtAuthorize
public class CurrentUserController {
    @Autowired
    private IIntegralsService integralsService;
    @Autowired
    private IUserService userService;
    @GetMapping("")
    public Callable<IResponseMessage> getCurrentUser(HttpServletRequest request){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                String auth =JwtUtils.getClaims(request,"auth").toString();

                if(null==auth)
                    throw new BadRequestException("请先登录");

                UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);
                IResponseMessage responseMessage =null;

                ServicesExcuteResult<UserInformationTransfer> result = userService.getCurrentInformation(userInformationTransfer.getId());
                if(result.isSuccess())
                {
                    responseMessage =new ResponseMessageWithData<UserInformationTransfer>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("integrals")
    public Callable<IResponseMessage> getIntergrals(HttpServletRequest request){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                String auth =JwtUtils.getClaims(request,"auth").toString();

                if(null==auth)
                    throw new BadRequestException("请先登录");

                UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);
                IResponseMessage responseMessage =null;

                ServicesExcuteResult<IntegralsTransfer> result = integralsService.queryIntegralByUserInformationId(userInformationTransfer.getId());
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<IntegralsTransfer>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PostMapping("attentions/{userInformationId}")
    public Callable<IResponseMessage> attentions(HttpServletRequest request,@PathVariable Long userInformationId){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                String auth =JwtUtils.getClaims(request,"auth").toString();

                if(null==auth)
                    throw new BadRequestException("请先登录");

                UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);
                IResponseMessage responseMessage =null;

                ServicesExcuteResult result = userService.attentionSomeOne(userInformationTransfer.getId(),userInformationId);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @DeleteMapping("attentions/{userInformationId}")
    public Callable<IResponseMessage> cancelAttentions(HttpServletRequest request,@PathVariable Long userInformationId){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                String auth =JwtUtils.getClaims(request,"auth").toString();

                if(null==auth)
                    throw new BadRequestException("请先登录");

                UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);
                IResponseMessage responseMessage =null;

                ServicesExcuteResult result = userService.cancelAttentionSomeOne(userInformationTransfer.getId(),userInformationId);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("attentions/{userInformationId}")
    public Callable<IResponseMessage> queryAttentions(HttpServletRequest request,@PathVariable Long userInformationId){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                String auth =JwtUtils.getClaims(request,"auth").toString();

                if(null==auth)
                    throw new BadRequestException("请先登录");

                UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);
                IResponseMessage responseMessage =null;

                ServicesExcuteResult<Boolean> result = userService.queryAttentsion(userInformationTransfer.getId(),userInformationId);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<Boolean>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @GetMapping("attentions")
    public Callable<IResponseMessage> queryAttentions(HttpServletRequest request,@Valid PageRequest pageRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                String auth =JwtUtils.getClaims(request,"auth").toString();

                if(null==auth)
                    throw new BadRequestException("请先登录");

                UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);
                IResponseMessage responseMessage =null;

                ServicesExcuteResult<PageResponse<UserInformationTransfer>> result = userService.getAttentions(userInformationTransfer.getId(),pageRequest);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<UserInformationTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @GetMapping("fans")
    public Callable<IResponseMessage> queryFans(HttpServletRequest request,@Valid PageRequest pageRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                String auth =JwtUtils.getClaims(request,"auth").toString();

                if(null==auth)
                    throw new BadRequestException("请先登录");

                UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);
                IResponseMessage responseMessage =null;

                ServicesExcuteResult<PageResponse<UserInformationTransfer>> result = userService.getFans(userInformationTransfer.getId(),pageRequest);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<UserInformationTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
